<?php

namespace app\service;

use app\client\Qrcode\SimSun;
use app\traits\Singleton;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Margin\Margin;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

class EwmService
{
    use Singleton;
    public function buildEwmString($content, $option = [])
    {
        $builder = Builder::create();
        $builder->writer(new PngWriter())
            ->writerOptions([])
            ->data($content)
            ->encoding(new Encoding('UTF-8'))
            ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
            ->size(400)
            ->margin(10);
        if (!empty($option['logo'])) {
            $builder->roundBlockSizeMode(new RoundBlockSizeModeMargin())
                ->logoPath(config_path() . '/image/logo.png')
                ->logoResizeToWidth(50);
        }
        if (!empty($option['label'])) {
            $builder->labelText($option['label'])
                ->labelFont(new SimSun(20))
                ->labelMargin(new Margin(-10, 0, 15, 0))
                ->labelAlignment(new LabelAlignmentCenter());
        }
        $result = $builder->validateResult(false)->build();
        return $result->getString();
    }
}